<?php

namespace app\merchant\controller\content;

use app\merchant\controller\Base;
use app\merchant\model\UploadFile as UploadFileModel;

/**
 * 文件库管理
 * Class Files
 * @package app\store\controller
 */
class Files extends Base
{
    /**
     * 文件列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new UploadFileModel;
        $list = $model->getList(-1, '', 0);
        return $this->renderSuccess($list,"查询成功");
    }

    /**
     * 回收站列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function recycle()
    {
        $model = new UploadFileModel;
        $list = $model->getList(-1, '', 1);
        return $this->renderSuccess($list,"查询成功");

    }

    /**
     * 移入回收站
     * @param $file_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function recovery()
    {
        $param = $this->postData();

        if(!isset($param['file_id']) || isInt($param['file_id'])){
            return $this->renderError([],'参数错误');
        }

        $model = UploadFileModel::detail($param['file_id']);
        if(!$model){
            return $this->renderError([],'该文件不存在，请刷新重试');
        }

        if (!$model->setRecycle(true)) {
            return $this->renderError([],$model->getError() ?: '操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }


    /**
     * 移出回收站
     * @param $file_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function restore()
    {
        $param = $this->postData();
        if(!isset($param['file_id']) || isInt($param['file_id'])){
            return $this->renderError([],'参数错误');
        }

        $model = UploadFileModel::detail($param['file_id']);
        if(!$model){
            return $this->renderError([],'该文件不存在，请刷新重试');
        }
        if (!$model->setRecycle(false)) {
            return $this->renderError([],'操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 删除文件
     * @param $file_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['file_id']) || isInt($param['file_id'])){
            return $this->renderError([],'参数错误');
        }

        $model = UploadFileModel::detail($param['file_id']);
        if(!$model){
            return $this->renderError([],'该文件不存在，请刷新重试');
        }
        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

}